package com.example.josan.rxbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @BindView(R.id.tv_tonext)
    TextView tvTonext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        RxBus.getInstance().register(User.class, User.RxBusTag.USER_CENTER)
                .subscribe(new Consumer<RxBusBean>() {
                    @Override
                    public void accept(RxBusBean rxBusBean) throws Exception {
                        User user = (User) rxBusBean;
                        Log.e(TAG, "accept1: user" + user.name);
                    }
                });


        RxBus.getInstance().register(User.class, User.RxBusTag.HOME_USER)
                .subscribe(new Consumer<RxBusBean>() {
                    @Override
                    public void accept(RxBusBean rxBusBean) throws Exception {
                        User user = (User) rxBusBean;
                        Log.e(TAG, "accept2: user" + user.name);
                    }
                });

        RxBus.getInstance().register(StringBean.class, StringBean.RxBusTag.USER_NAME)
                .subscribe(new Consumer<RxBusBean>() {
                    @Override
                    public void accept(RxBusBean rxBusBean) throws Exception {
                        StringBean stringBean = (StringBean) rxBusBean;
                        Log.e(TAG, "name: " + stringBean.content);
                    }
                });

        RxBus.getInstance().register(StringBean.class, StringBean.RxBusTag.PWD)
                .subscribe(new Consumer<RxBusBean>() {
                    @Override
                    public void accept(RxBusBean rxBusBean) throws Exception {
                        StringBean stringBean = (StringBean) rxBusBean;
                        Log.e(TAG, "pwd:" + stringBean.content);
                    }
                });

        RxBus.getInstance().register(EmptyBean.class, EmptyBean.RxBusTag.REFRESH_DATA)
                .subscribe(new Consumer<RxBusBean>() {
                    @Override
                    public void accept(RxBusBean rxBusBean) throws Exception {
                        Log.e(TAG, "刷新数据");
                    }
                });

        RxBus.getInstance().register(EmptyBean.class, EmptyBean.RxBusTag.CLOSE_ACTIVITY)
                .subscribe(new Consumer<RxBusBean>() {
                    @Override
                    public void accept(RxBusBean rxBusBean) throws Exception {
                        Log.e(TAG, "关闭Activity");
                    }
                });


    }

    @OnClick(R.id.tv_tonext)
    public void onViewClicked() {
        Intent intent = new Intent(MainActivity.this, NextActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getInstance().unregisterAll();
    }
}
